Skip to content

feat(tenants): add opt-in organisations, teams and verified workspaces - #70

Merged
ALikesToCode merged 2 commits into
integrate/gateway-waves-1-6from
feat/tenant-hierarchy
Oct 9, 2026
Merged

ALikesToCode merged 2 commits into
integrate/gateway-waves-1-6from
feat/tenant-hierarchy

Conversation

@ALikesToCode

Copy link
Copy Markdown
Owner

Summary

Adds optional organisations and teams with a workspace identity the server verifies. With ORGANISATIONS_ENABLED unset or off (the default), every route, response, account read, key record and storage operation is byte-for-byte unchanged, the new routes return 404, and every request keeps the legacy tenant context with an empty namespace.

  • Settings: ORGANISATIONS_ENABLED. Empty means off; a malformed value is off with one warning that leaves the value out.
  • Hierarchy:
    • Exactly two levels, organisation and team, with generated opaque IDs, active/deactivated status and integer revisions.
    • Memberships link an existing account to an organisation and optionally one of its teams, with role admin, billing or member. These roles grant nothing on the gateway administrator dashboard.
    • At most 100 teams and 1,000 memberships per organisation (409 tenant_limit_reached). Nothing is deleted; rows are deactivated.
  • Administration (administrator dashboard session and CSRF):
    • GET/POST /admin/organisations, GET/PATCH /admin/organisations/<org_id>;
    • GET/POST /admin/organisations/<org_id>/teams, PATCH /admin/organisations/<org_id>/teams/<team_id>;
    • GET /admin/organisations/<org_id>/members, PUT /admin/organisations/<org_id>/members/<principal>.
    • Updates need If-Match: missing returns 428, stale returns 412. Every change writes an audit row.
  • Workspaces:
    • Each account has at most one workspace binding, stored on the server.
    • GET /v1/workspaces lists the caller's active memberships. POST /v1/workspaces/switch changes the caller's own binding only after a membership check.
    • Caller headers such as X-Org or X-Team never choose a workspace.
    • Accounts without a binding stay in the legacy namespace. Nothing is migrated or reparented automatically.
  • Runtime: a new tenant_context_hook runs first after authentication. A missing workspace returns 404 workspace_not_found, and a deactivated workspace or revoked membership returns 403 workspace_forbidden, before any storage, cache, claim, admission or provider call.
  • Storage: standalone SQLite or D1 (migration 0033). Missing tables with the flag on return 503 tenant_storage_unavailable for every authenticated request; there is no silent fallback.

Details are in docs/tenant-hierarchy.md.

Wiring

  • app.py mounts routes/tenants.py. services/gateway_extensions.py registers tenant_context_hook first in the named order.
  • services/auth_service.py gets two call sites: the workspace is resolved after the key is verified and before key-usage metadata is written.
  • Worker:
    • worker/managed-state-dispatch.mjs routes the private /v1/managed-state/tenants domain to worker/tenants-d1.mjs; it accepts only the internal origin and returns 404 while off.
    • Native generations, knowledge and Realtime resolve the same context, only for the account whose key matched. With the flag off they skip resolution entirely.
  • Adding the namespace to cache keys, idempotency, hosted state and usage rows comes with the Wave 7 integration. Until then a bound principal's caches and usage still use the legacy partition.

Review fix

The first version resolved a workspace for every account that shared the caller's 8-character key prefix, before the key was compared. Another account's deactivated workspace could then deny the caller, and the error became a 503-style failure. Resolution now happens only after the key matches, and tests cover two accounts sharing a prefix.

Shared registrations

  • tests/test_tenant_hierarchy_worker.mjs in test:worker.
  • worker/tenants-d1.mjs in the edge module inventory, and the eight routes in routes/tenants.py in the Flask route inventory.
  • ORGANISATIONS_ENABLED in the Container env keys.
  • tests/test_d1_integration.mjs now starts its Worker with nodejs_compat, as wrangler.jsonc does in production. worker/tenants-d1.mjs imports node:crypto, and without the flag the integration Worker failed to start.
  • tenant_organisations, tenant_teams, tenant_memberships, tenant_bindings and tenant_audit in REQUIRED_D1_TABLES.

Stack

This is the bottom of the Wave 7 stack (migration 0033), based on integrate/gateway-waves-1-6 (#69). Tenant governance (0034), SAML federation (0035), SCIM provisioning (0036), the credits ledger (0037) and payment billing (0038) stack on it in that order.

Tests

  • tests/test_tenant_hierarchy.py and tests/test_tenant_hierarchy_worker.mjs cover:
    • flag off, empty and malformed leaving responses and storage unchanged;
    • membership isolation, foreign IDs and caps;
    • ignored workspace headers and body fields;
    • revision checks and invalidation;
    • deactivation without reparenting or deleting old records;
    • identical namespaces on the native and Flask paths;
    • the prefix-collision case above.
  • Full Python suite: 4,790 passed, 2 skipped, 1 failed. The failure, test_registered_native_protocols_preserve_bytes in tests/test_output_schema_validation.py, only appears in the full run: it passes alone and after the tenant tests, and passed in the full run of every layer stacked on this one. ruff, mypy and bandit are clean.
  • npm run test:worker: 1,793 passed. npm run test:integration: 7 passed.
  • cf:migrations:check reports 33 contiguous, additive migrations. test:deploy, free routes, client headers and the wrangler deploy --dry-run bundle all pass.
  • tests/test_frontend_build.mjs fails locally because @tailwindcss/cli is not installed. It fails the same way on main.

Apply the migration before enabling the flag: npx wrangler d1 migrations apply multillm-intelligence --remote. For standalone SQLite, apply 0033_tenant_hierarchy.sql to the account database.

CI notes

  • GitHub python/node jobs: the dependency-audit steps fail on every branch, including main. fix(deps): clear the CI dependency audit advisories #42 fixes that.
  • Workers Builds: fails on this stack because the Wave 3 base adds the AdmissionCoordinator Durable Object migration, which wrangler versions upload cannot apply. This is expected until wrangler deploy runs on main.

This is part of Wave 7. The README line comes with the Wave 7 integration PR.

ORGANISATIONS_ENABLED (off by default) adds a two-level organisation and team
hierarchy with stable opaque IDs, roles, caps and deactivation instead of
deletion. Administrators manage it under /admin/organisations with
If-Match revisions and audit rows. Each account has at most one server-side
workspace binding; /v1/workspaces lists the caller's memberships and
/v1/workspaces/switch changes the binding only after a membership check.

A new first post-authentication hook resolves the verified TenantContext and
refuses a missing or deactivated workspace with 404 or 403 before any
storage, cache, claim or provider call. Caller headers never choose a
workspace. Native Worker generations, knowledge and Realtime resolve the
same context, only after the key matches. Accounts without a binding stay
in the legacy namespace (migration 0033).
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
multillm-proxy 0cfdf92 Oct 09 2026, 07:58 AM

@coderabbitai

coderabbitai Bot commented Oct 9, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fd7db94d-a337-4ea0-b212-a8b2a2bdb244

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant